using Holoville.HOTween;
using Holoville.HOTween.Core;
using UnityEngine;

[ExecuteInEditMode]
[RequireComponent(typeof(LineRenderer))]
public class LineTracer : MonoBehaviour
{
	[SerializeField]
	public float lenght;

	private bool inLastPos;

	private LineRenderer line;

	private Vector3 lastPos;

	private float distance;

	private void Start()
	{
	}

	public void AnimateLenght(Vector3 start, Vector3 end, float speed, float scaleFactor)
	{
		line = GetComponent<LineRenderer>();
		inLastPos = true;
		base.transform.position = end;
		lenght = Vector3.Distance(end, start);
		line.SetPosition(0, end);
		line.SetPosition(1, start);
		base.transform.localScale *= scaleFactor;
		HOTween.To(this, Vector3.Distance(end, start) / speed, new TweenParms().Prop("lenght", 0).Ease(EaseType.Linear).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			Object.Destroy(base.gameObject);
		})
			.OnUpdate((TweenDelegate.TweenCallback)delegate
			{
				line.SetPosition(1, base.transform.position - (end - start).normalized * lenght);
			}));
	}

	private void FixedUpdate()
	{
		if (inLastPos)
		{
			return;
		}
		if (lastPos == Vector3.zero)
		{
			line = GetComponent<LineRenderer>();
			lastPos = base.transform.position;
			return;
		}
		line.SetPosition(0, base.transform.position);
		if (distance > lenght * 2f)
		{
			line.SetPosition(1, base.transform.position - base.transform.forward * Mathf.Clamp(Vector3.Distance(base.transform.position, lastPos), lenght, 1000f));
		}
		else
		{
			line.SetPosition(1, base.transform.position - base.transform.forward * Mathf.Clamp(distance - Vector3.Distance(base.transform.position, lastPos), 0f, lenght));
			distance += Vector3.Distance(base.transform.position, lastPos);
		}
		lastPos = base.transform.position;
	}

	private void DestroyAfterDelay()
	{
		Object.Destroy(base.gameObject);
	}
}
